class DeleteRequestsController < ApplicationController
  filter_access_to :all
  
  #If AJAX request, use the slim layout (no header or footer)
  #Otherwise use the main layout
  layout Proc.new { |controller| controller.request.xhr? ? 'slim' : 'main' }

  # GET /delete_requests
  def index
    @requests = Edit.all(:conditions => ["moderated = ?", false])
  end

  # GET /delete_request/:id
  def show
    @request = Edit.find(params[:id])
  end

  # GET /delete_request/new
  def new
    @request = Edit.new
  end

  # POST /delete_request
  def create
    @request = Edit.new(params[:edit])
    @request.user_id = current_user.id
    @request.approved = false
    @request.moderated = false
    @request.change = Hash.new
    @request.change.store("delete", [@request.editable])

    if @request.save!
      redirect_to_request_uri
    else
      render :new
    end
  end

  # GET /delete_request/:id/edit
  def edit
    @request = Edit.find(params[:id])
  end

  # PUT /delete_request/:id
  def update
    @request = Edit.find(params[:id])
    @request.moderated = true

    if @request.update_attributes(params[:edit])
      if @request.approved?
        @request.editable.destroy
      end
        redirect_to delete_requests_path
    else
      render :edit
    end
  end 

end
